package org.cresource.basic.util;

import org.cresource.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录上下文
 * 1.获取当前登录人的信息
 * 2.获取当前登录人的权限（我们不写）
 *
 */
public class LoginContext {


    /**
     * 通过从请求头中获得token 用过tokne去redis拿到logininfo
     */
    public static LoginInfo getLoginInfo(HttpServletRequest request){
        //获取token
        String token = request.getHeader("token");
        //获取spring容器
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        RedisTemplate redisTemplate = applicationContext.getBean("redisTemplate", RedisTemplate.class);
        //根据token从redis中拿到logininfo
        Object o = redisTemplate.opsForValue().get(token);
        if (o!=null){
            return (LoginInfo)o;
        }
        return null;


    }



}
